//
//  XLChannelItem.m
//  XLChannelControlDemo
//
//  Created by MengXianLiang on 2017/3/3.
//  Copyright © 2017年 MengXianLiang. All rights reserved.
//

#import "XLChannelItem.h"

@interface XLChannelItem ()
{
    UILabel *_textLabel;
    
    CAShapeLayer *_borderLayer;
}
@end

@implementation XLChannelItem

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}

-(void)initUI
{
    _textLabel = [UILabel new];
    _textLabel.frame = self.bounds;
    _textLabel.backgroundColor = [UIColor themeColor];
    _textLabel.textAlignment = NSTextAlignmentCenter;
    _textLabel.textColor = [UIColor whiteColor];
    _textLabel.adjustsFontSizeToFitWidth = true;
    _textLabel.userInteractionEnabled = true;
    _textLabel.font = LYPFONT(15);
    [self addSubview:_textLabel];
    
    [self addBorderLayer];
    
    [self addSubview:self.deleteButton];
}

-(void)addBorderLayer{
    _borderLayer = [CAShapeLayer layer];
    _borderLayer.bounds = self.bounds;
    _borderLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    _borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:_borderLayer.bounds cornerRadius:self.layer.cornerRadius].CGPath;
    _borderLayer.lineWidth = 1;
    _borderLayer.lineDashPattern = @[@5, @3];
    _borderLayer.fillColor = [UIColor clearColor].CGColor;
    _borderLayer.strokeColor = [UIColor textGrayColor].CGColor;
    [self.layer addSublayer:_borderLayer];
    _borderLayer.hidden = YES;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    _textLabel.frame = self.bounds;
}


#pragma mark -
#pragma mark Setter

-(void)setModel:(YBMeetingReturnItemModel *)model
{
    _model = model;
    _textLabel.text = [ToolClass returnNoNullStringWithString:model.CODE_NAME];
    if (model.isSelected) {
        _textLabel.backgroundColor = [UIColor themeColor];
        _textLabel.textColor = [UIColor whiteColor];
        _textLabel.layer.borderWidth = 0;
        _textLabel.layer.borderColor = [UIColor themeColor].CGColor;
    }
    else{
        _textLabel.backgroundColor = [UIColor themeGrayColor];
        _textLabel.textColor = [UIColor textGrayColor];
        _textLabel.layer.borderWidth = 1;
        _textLabel.layer.borderColor = [UIColor textBlackColor].CGColor;
    }
    
}



-(void)setIsMoving:(BOOL)isMoving
{
    _isMoving = isMoving;
    if (_isMoving) {
        _textLabel.backgroundColor = [UIColor clearColor];
        _textLabel.textColor = [UIColor textGrayColor];
        _borderLayer.hidden = NO;
    }else{
        _textLabel.backgroundColor = [UIColor themeColor];
        _textLabel.textColor = [UIColor whiteColor];
        _borderLayer.hidden = YES;
    }
}


-(UIButton *)deleteButton
{
    if (!_deleteButton) {
        _deleteButton = [ToolClass createButtonWithFrame:CGRectMake(0, 0 , ItemWidth * 20, ItemWidth * 20) text:@"" textColor:[UIColor whiteColor] imageName:@"del_people" font:16];
        _deleteButton.center = CGPointMake(self.bounds.size.width, 0);
        _deleteButton.hidden = YES;
    }
    return _deleteButton;
}

@end
